package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.Result;
import com.itheima.entity.AddressBook;
import com.itheima.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    /*
    查询用户的地址信息
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> addressBookList(AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(addressBook.getUserId()!=null,AddressBook::getUserId,addressBook.getUserId());
        lambdaQueryWrapper.orderByDesc(AddressBook::getIsDefault);
        List<AddressBook> addressBookList = addressBookService.list(lambdaQueryWrapper);
        return Result.success(addressBookList);
    }
    /*
    根据id查询地址信息
     */
    @GetMapping("/{id}")
    public Result<AddressBook> addressBookById(@PathVariable Long id){
        log.info(id.toString());
        AddressBook byId = addressBookService.getById(id);
        return Result.success(byId);
    }
    /*
    修改根据id地址信息
     */
    @PutMapping
    public Result<String> editAddressBook(@RequestBody AddressBook addressBook){
        log.info(addressBook.toString());
        addressBookService.updateById(addressBook);
        return Result.success("修改成功！");
    }
    /*
    根据id删除地址信息
     */
    @DeleteMapping
    public Result<String> deleteAddress(Long ids){
        log.info(ids.toString());
        addressBookService.removeById(ids);
        return Result.success("删除地址成功！");
    }
    /*
    新增地址
     */
    @PostMapping
    public Result<String> addressBooksave(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookService.save(addressBook);
        return Result.success("新增地址成功！");
    }
    /*
    修改默认地址
     */
    @PutMapping("/default")
    public Result<AddressBook> defaultAddress(@RequestBody AddressBook addressBook){
        log.info(addressBook.toString());
        Long userid = BaseContext.getCurrentId();
        LambdaUpdateWrapper<AddressBook> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(AddressBook::getUserId,userid);
        lambdaUpdateWrapper.set(AddressBook::getIsDefault,0);
        addressBookService.update(lambdaUpdateWrapper);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return Result.success(addressBook);
    }
    @GetMapping("/default")
    public Result<AddressBook> def(AddressBook addressBook){
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook bookServiceOne = addressBookService.getOne(queryWrapper);
        if (bookServiceOne == null) {
            Result.error("查询失败");
        }
        return Result.success(bookServiceOne);
    }
}
